<?php
/**
 * Created by PhpStorm.
 * Login: Administrator
 * Date: 2017-10-18
 * Time: 15:12
 */
namespace app\index\controller;
use think\Controller;
use think\Db;

class Login extends Controller
{//登录页面
    public function index()
    {
        return $this->fetch();
    }

    public function login()
    {//登录数据处理

        $login['ch_name'] = input('post.ch_name');
        $login['password'] = md5(input('post.password'));
        $where['password'] = md5(input('post.password'));
        $where['en_name'] = input('post.ch_name');
        $result1 = Db::table('oa_user')->where($login)->where('state=1')->select();
        $result2 = Db::table('oa_user')->where($where)->where('state=1')->select();
        //dump(count($result));die;
        if (!empty($result1)) {
            if (count($result1) == 1) {
                session_start();
                $_SESSION['ch_name'] = $result1[0]['ch_name'];
                $_SESSION['en_name'] = $result1[0]['en_name'];
                $_SESSION['id'] = $result1[0]['id'];
                //$session=Db::table('oa_user_session')->where(['user_id'=>$result1[0]['id']])->find();
                $time = time();
                $data_entry = date('Y-m-d H:i:s', $time);
                $_SESSION['time'] = $data_entry;
                $wheres['user_id'] = $result1[0]['id'];
                $wheres['enter_time'] = $data_entry;
                $res = Db::table('oa_user_entertime')->insert($wheres);
                if ($res) {
                    //dump(234);die;
                    $this->redirect('Index/index');
                }
                //$data_end = date('Y-m-d H:i:s', $time+900);
                //$wheres['end_time'] =$data_end;
            } else {
                //dump(123);die;
                $this->redirect('Login/index');
                //$this->error('登录失败,请联系IT部门', 'Login/admin');
            }
        } elseif (!empty($result2)) {
            if (count($result2) == 1) {
                session_start();
                $_SESSION['ch_name'] = $result2[0]['ch_name'];
                $_SESSION['en_name'] = $result2[0]['en_name'];
                $_SESSION['id'] = $result2[0]['id'];
                $time = time();
                $data_entry = date('Y-m-d H:i:s', $time);
                $_SESSION['time'] = $data_entry;
                $wheres['user_id'] = $result2[0]['id'];
                $wheres['enter_time'] = $data_entry;
                $res = Db::table('oa_user_entertime')->insert($wheres);
                if ($res) {
                    $this->redirect('Index/index');
                }
            } else {
                $this->redirect('Login/index');
                //$this->error('登录失败,请联系IT部门', 'Login/admin');
            }
        } else {
            $this->redirect('Login/index');
            //$this->error('登录失败', 'Login/admin');
        }
    }

    public function regist()
    {
        //退出登录
        session_start();
        $enter_time = $_SESSION['time'];
        $user_id = $_SESSION['id'];
        $time = time();
        $end_time['end_time'] = date('Y-m-d H:i:s', $time);
        $where['enter_time'] = $enter_time;
        Db::table('oa_user_entertime')->where($where)->update($end_time);
        Db::table('oa_user_session')->where(['user_id' => $user_id])->update(['sessionid' => $time]);
        unset($_SESSION['ch_name']);
        unset($_SESSION['en_name']);
        unset($_SESSION['time']);
        unset($_SESSION['id']);
        $this->redirect('Login/index');
    }
}